package lib.quasar.util;

import android.util.DisplayMetrics;
import android.util.TypedValue;

import lib.quasar.context.BaseApp;

/**
 * description: 尺寸
 * created by kalu on 2018/5/6 19:35
 */
public final class DimenUtil {

    public static float sp2px(float sp) {

        final DisplayMetrics metrics = BaseApp.getContext().getResources().getDisplayMetrics();
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp, metrics);
    }

    public static float px2sp(float px) {

        final DisplayMetrics metrics = BaseApp.getContext().getResources().getDisplayMetrics();
        return px / metrics.scaledDensity;
    }

    public static float dp2px(float dp) {

        final DisplayMetrics metrics = BaseApp.getContext().getResources().getDisplayMetrics();
        return dp * metrics.density;
    }

    public static int dp2px(int dp) {

        final DisplayMetrics metrics = BaseApp.getContext().getResources().getDisplayMetrics();
        return (int) (dp * metrics.density);
    }

    public static float px2dp(float px) {

        final DisplayMetrics metrics = BaseApp.getContext().getResources().getDisplayMetrics();
        return px / metrics.density;
    }
}
